<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2018 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 老猫 <thinkcmf@126.com>
// +----------------------------------------------------------------------
namespace app\blog\model;
use think\Model;
use think\db;

class SetmealModel extends Model
{
    public function __construct($data = [])
    {
        parent::__construct($data);
        $this->id=cmf_get_current_admin_id();
    }
    public  function session()
    {
        $id=cmf_get_current_admin_id();
        $cause['id']=$id;
        $user=DB::name('user')->where($cause)->find();
        if($user['user_type']==1)
        {
            $where='';
            $session=DB::name('shop')->where($where)->select();
        }else{
            $where['re_guid']=$user['shop_guid'];
            $session=DB::name('shop')->where($where)->select()->toArray();
        }
        return $session;
    }
   /*套餐*/
   public function index($cause)
   {
       $session=$this->session();
       if($session)
       {
           if($this->id==1)
           {
               $where='';
           }else{
               $where['se.shop_id']=$session[0]['re_guid'];
           }
           if($cause)
           {
               $where['se.set_name']=array('like',"%".$cause."%");
           }
           $data=DB::name('setmeal')->alias('se')->join('shop sp','sp.re_guid = se.shop_id')->where($where)->field('se.*,sp.shop_name')->paginate(10);
           if($data->toArray())
           {
               foreach ($data as $k=>$v)
               {
                   $data->toArray();
                   $date=array();
                   $date=$v;
                   if($v['status']==1){
                       $date['status_name']='启用';
                   }else{
                       $date['status_name']='禁用';
                   }
                   $data->offsetSet($k,$date);
               }

           }
           $data->appends(['set_name'=>$cause]);
           $array['data']=$data;
           $array['page']=$data->render();
       }else{
           $array['data']="";
           $array['page']="";
       }

       return $array;

   }
   /*商品*/
   public function addgoods($cause=null)
   {
       $session=$this->session();
       if($session)
       {
           if(isset($cause['goods_name']))
           {
               $where['goods_name']=['like',"%".$cause['goods_name']."%"];
               $name=$cause['goods_name'];
           }else{
               $name='';
           }
           if($this->id !=1)
           {
               $where['shop_id']=$session[0]['re_guid'];
           }
           $where['status']=1;
           $date=DB::name('goods')->where($where)->paginate(10);
           $date->appends(['goods_name'=>$name,'status'=>1]);
           $array['data']=$date;
           $array['page']=$date->render();
       }else{
           $array['data']="";
           $array['page']="";
       }
       return $array;
   }
   /*
    * 套餐添加
    * */
   public function add($data)
   {
     if(isset($data['shop']))
     {
         $array['set_guid']=guid();
         $array['set_name']=$data['set_name'];
         $array['set_price']=$data['set_price'];
         $array['set_prices']=$data['set_prices'];
         $array['number']=$data['number'];
         $array['goods_id']=$data['goods_id'];
         $array['status']=$data['status'];
         $array['shop_id']=$data['shop'];
         $array['c_time']=time();
         $status=Db::name('setmeal')->insert($array);
     }else{
         $status=0;
     }
       return $status;
   }
   /*修改套餐*/
   public function detail($date)
   {
       $where['set_guid'] = $date['set_guid'];
       $data['set_name']=$date['set_name'];
       $data['set_price']=$date['set_price'];
       $data['set_prices']=$date['set_prices'];
       $data['goods_id']=$date['goods_id'];
       $data['number']=$date['number'];
       $data['status']=$date['status'];
       $data['c_time']=time();
        $status=DB::name('setmeal')->where($where)->update($data);
     return $status;
   }
   /*
    * 删除
    * 先判断是否有该类别的商品 然后看 是否有该类别的订单
    * */
   public function del($cause)
   {

           $where['goods_id']=$cause['set_guid'];
           $order=DB::name('order_detail')->where($where)->find();
           if($order)
           {
               $status['num']=0;
               $status['con']='套餐下有订单，不能删除！';
           }else{
               $goodsclass=DB::name('setmeal')->where($cause)->delete();
               if($goodsclass)
               {
                   $status['num']=1;
                   $status['con']='删除成功';
               }else{
                   $status['num']=0;
                   $status['con']='删除失败！';
               }
           }
       return $status;
   }
   /**
    * 根据id获取列表记录
   */
   public function getgcone($where){
       $session=$this->session();
       if($this->id != 1)
       {
           $where['shop_id']=$session[0]['re_guid'];
       }else{
           $where="";
       }
       $status= DB::name('setmeal')->where($where)->find();
       $goods_id=explode(',',$status['goods_id']);
       $good_name='';
       foreach ($goods_id as $k=>$v)
       {
           if($v)
           {
               $cause['goods_guid']=$v;
               $good_name[$k]=$this->getgoods($cause);
           }
       }
       $good_name=implode(',',$good_name);
       $status['goods_name']=$good_name;
       return $status;
   }
   /*单个商品*/
   public function getgoods($where)
   {
       $goods=DB::name('goods')->where($where)->field('goods_name')->find();
       return $goods['goods_name'];
   }

}